package enrique.kurso;

import java.util.ArrayList;
import java.util.List;

import enrique.kurso.lecionoj.Leciono01;
import enrique.kurso.util.ProgressView;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends ListActivity {
	 private ListAdapter m_adapter;
	    
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.list);

	        List<String> options=new ArrayList<String>();
	        options.add(getResources().getString(R.string.leciono1));
	        options.add(getResources().getString(R.string.leciono2));
	        options.add(getResources().getString(R.string.leciono3));
	        options.add(getResources().getString(R.string.leciono4));
	        options.add(getResources().getString(R.string.leciono5));
	        options.add(getResources().getString(R.string.leciono6));
	        options.add(getResources().getString(R.string.leciono7));
	        options.add(getResources().getString(R.string.leciono8));
	        options.add(getResources().getString(R.string.leciono9));
	        options.add(getResources().getString(R.string.leciono10));
	        options.add(getResources().getString(R.string.leciono11));
	        options.add(getResources().getString(R.string.leciono12));
	        
	        
	        this.m_adapter = new ListAdapter(this, R.layout.optionslist,options);
	        setListAdapter(this.m_adapter);
	    }

	   protected void onResume() {
		   super.onResume();
		   ((ListAdapter) this.getListAdapter()).notifyDataSetChanged();
	    }
	 
	    protected void onListItemClick(ListView l, View v, int position, long id) {
	        Intent intent=new Intent(this, Leciono01.class);
	        intent.putExtra("exerciseType", position+1);
	        startActivity(intent);
	    }
	    
	    private class ListAdapter extends ArrayAdapter<String> {

	    	
	        private List<String> items;
	        private Context context;

	        public ListAdapter(Context context, int textViewResourceId, List<String> items) {
	                super(context, textViewResourceId, items);
	                this.items = items;
	                this.context = context;
	        }

	        public void setItems(List<String> items) {
	                this.items = items;
	                
	        }
	        
	        @Override
	        public View getView(int position, View convertView, ViewGroup parent) {
	                View v = convertView;
	                if (v == null) {
	                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	                    v = vi.inflate(R.layout.optionslist, null);
	                }
	                String titulo = items.get(position);
	                if (titulo!=null) {
	                        TextView tt = (TextView) v.findViewById(R.id.toptext);
	                        if (tt != null) {
	                              tt.setText(titulo);
	                        }

	                        int fallos=0;
	                        int preguntas=0;
	                        int respuestas=0;
	                    /*    List<Ejercicio> ejercicios=Ejercicio.getByTipo(context,position+1);
	                        for(int i=0;i<ejercicios.size();i++){
	                        	fallos+=ejercicios.get(i).getFallos();
	                        	preguntas+=ejercicios.get(i).getPreguntas();
	                        	respuestas+=ejercicios.get(i).getRespuestas();
	                        }
	                        */
	                        ProgressView progress = (ProgressView) v.findViewById(R.id.exerciseProgress);
	                        if(progress != null){
	                        	if(preguntas>0 && respuestas>0)
	                        		progress.setPercent(respuestas*100/(preguntas*4),100-900/(9+fallos));
	                        	else progress.setPercent(0,0);
	                        }
	                }
	                return v;
	        }
	}

}